package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.common.BaseResponse;
import com.example.yaja.yaja.common.ResponseBuilder;
import com.example.yaja.yaja.dao.TravelsMapper;
import com.example.yaja.yaja.entity.Idea;
import com.example.yaja.yaja.entity.Travels;
import com.example.yaja.yaja.service.TravelsService;
import com.example.yaja.yaja.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * Created by Zero on 2017/12/5.
 */
@RestController
@RequestMapping("/travels")
public class travelsController {

    @Autowired
    TravelsService travelsService;
    @Autowired
    UserService userService;

    @Autowired
    TravelsMapper travelsMapper;


    /**
     * 查找游记记录
     * @return
     */
    @RequestMapping(value = "/show/{count}",method = RequestMethod.GET)
    public Map<String,Object> findTravels(@PathVariable int count){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Travels> travelses=travelsService.findTravelsByNumber(count*8,8);
        if(travelses!=null){
            for(int i=0;i<travelses.size();i++){
                int userId=travelses.get(i).getUserId();
                String name=userService.findUserById(userId).getName();
                String time= DateFormat.getDateInstance().format(travelses.get(i).getTime());
                travelses.get(i).setTimeA(time);
                travelses.get(i).setUserName(name);
            }
            map.put("travelses",travelses);
        }else {
            map.put("travelsesState",0);
        }
        return map;
    }


    /**
     * 查找游记记录
     * @return
     */
    @RequestMapping(value = "/showPersonal",method = RequestMethod.GET)
    public Map<String,Object> findTravelsPersonal(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Travels> travelses=travelsService.findTravelsByNumber(0, 10);
        if(travelses!=null){
            for(int i=0;i<travelses.size();i++){
                int userId=travelses.get(i).getUserId();
                String name=userService.findUserById(userId).getName();
                String time= DateFormat.getDateInstance().format(travelses.get(i).getTime());
                travelses.get(i).setTimeA(time);
                travelses.get(i).setUserName(name);
            }
            map.put("travelses",travelses);
        }else {
            map.put("travelsesState",0);
        }
        return map;
    }



    /**
     * 添加游记
     * @param travels
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public int addTravels(@RequestBody Travels travels,HttpSession userName){

        int result=0;
        Object userId=userName.getAttribute("userId");
        if(travels!=null&&userId!=null){
            travels.setUserId((Integer) userId);
            travels.setTime(new Date());
            int travelsId=travelsService.addTravels(travels);
            result=travelsId;
        }else {
            result=0;
        }
        return result;
    }



    /**
     * 查找个人游记记录
     * @return
     */
    @RequestMapping(value = "/personal/{uId}",method = RequestMethod.GET)
    public Map<String,Object> findPersonalTravels(@PathVariable int uId){

        Map<String,Object> map = new HashMap<String, Object>();
        List<Travels> travelses=travelsService.findTravelsByUserId(uId);
        if(travelses!=null){
            for(int i=0;i<travelses.size();i++){
                String name=userService.findUserById(uId).getName();
                String time= DateFormat.getDateInstance().format(travelses.get(i).getTime());
                travelses.get(i).setTimeA(time);
                travelses.get(i).setUserName(name);
            }
            map.put("travelses",travelses);
        }else {
            map.put("travelsesState",0);
        }
        return map;
    }

    /**
     * 查询所有游记
     * @param currPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/queryAllTravels", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse queryAllTravels(@RequestParam Integer currPage,
                                        @RequestParam Integer pageSize){
        ResponseBuilder custom = ResponseBuilder.custom();
        List<Travels> travelsList=travelsMapper.queryAllTravels();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            if(travelsList!=null){
                for(int i=0;i<travelsList.size();i++){
                    int userId=travelsList.get(i).getUserId();
                    String name=userService.findUserById(userId).getName();
                    String time= DateFormat.getDateInstance().format(travelsList.get(i).getTime());
                    travelsList.get(i).setTimeA(time);
                    travelsList.get(i).setUserName(name);
                }
                custom.data(travelsList).currPage(currPage).pageSize(pageSize).totalCount(travelsList.size());
            }else {
                custom.failed("查询失败", -1);
            }
        } catch (Exception e) {
            custom.failed("查询失败", -1);
        }
        return custom.build();
    }

    @RequestMapping(value = "/delTravels", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse delIdea(Integer travelsId){
        ResponseBuilder custom = ResponseBuilder.custom();
        int result=travelsMapper.deleteByPrimaryKey(travelsId);

        if (result!=0){
            custom.success("成功",0);
        }else{
            custom.failed("失败", -1);
        }
        return custom.build();
    }

    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateState(Integer travelsId,Integer state){
        ResponseBuilder custom = ResponseBuilder.custom();
        int result=travelsService.updateState(travelsId,state);

        if (result!=0){
            custom.success("成功",0);
        }else{
            custom.failed("失败", -1);
        }
        return custom.build();
    }
}
